﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Model;
using MT.eVotePRS.Presentation;
using MT.eVotePRS.Presentation.Presenter;

namespace MT.eVotePRS.UI.UserControls
{
    public partial class Lost : UserControl, IViewLost
    {
        #region IViewLost

        Handsets _handset;
        StudentInfo _student;
        InventoryTxn _txn;

        Status _toStatus;
        public Status ToStatus { get { return _toStatus; } }

        public Handsets Handset
        {
            get { return _handset; }
            set
            {
                _handset = value;
                txtHandsetID.Text = _handset.MAC;
            }
        }

        public StudentInfo Student
        {
            get { return _student; }
            set
            {
                _student = value;
                txtBarcode.Text = _student.Barcode;
                txtStudentID.Text = _student.StudentNo;
                txtStudentName.Text = _student.StudentName;
            }
        }

        public InventoryTxn Txn
        {
            get { return _txn; }
            set { _txn = value; }
        }

        public string EventRemark { get { return txtRemark.Text; } }

        public event EventHandler BarcodeEntered;
        public event EventHandler StudentNoEntered;
        public event EventHandler ConfirmClicked;

        public void SetError(string propertyName, string errorMessage)
        {

        }


        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        // Event firing helpers
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handlers = PropertyChanged;
            if (handlers != null)
            {
                handlers(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected virtual void RaiseBarcodeEntered()
        {
            EventHandler handlers = BarcodeEntered;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaiseStudentNoEntered()
        {
            EventHandler handlers = StudentNoEntered;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }

        protected virtual void RaiseConfirmClicked()
        {
            EventHandler handlers = ConfirmClicked;
            if (handlers != null)
            {
                handlers(this, EventArgs.Empty);
            }
        }


        public Lost(Status toStatus)
        {
            InitializeComponent();

            this.btnConfirm.Click += OnConfirmClicked;
            this.txtBarcode.KeyDown += OnKeyDown;
            this._toStatus = toStatus;
        }

        void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (sender == txtBarcode)
                {
                    RaiseBarcodeEntered();
                }

                if (sender == txtStudentID)
                {
                    RaiseStudentNoEntered();
                }
            }
        }

        void OnConfirmClicked(object sender, EventArgs e)
        {
            RaiseConfirmClicked();
        }
    }
}
